//$URL: http://dasding.googlecode.com/svn/branches/testzeugs/data/dao/mongoDB/src/main/java/de/piratech/dasding/data/database/mongo/ActionDataImpl.java $
//$Id: ActionDataImpl.java 94 2013-03-24 22:24:17Z krieg.maria@gmail.com $
/*
 * Copyright 2012 Piratech.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package de.piratech.dasding.data.database.mongo;

import java.util.ArrayList;
import java.util.List;

import com.mongodb.DB;

import de.piratech.dasding.data.Action;
import de.piratech.dasding.data.database.ActionData;
import de.piratech.dasding.data.database.DatabaseException;

/**
 */
public class ActionDataImpl extends AbstractMongoDB<Action> implements
    ActionData {

  protected ActionDataImpl(DB pDB) {
    super(pDB, Action.class, COLLECTIONS.ACTION_COLLECTION);
  }

  @Override
  public Action createAction(Action pAction) {
    return getDBCollection().insert(pAction).getSavedObject();
  }

  @Override
  public void deleteAction(Action pAction) {
    getDBCollection().removeById(pAction.getId());
  }

  @Override
  public void updateAction(Action pAction) {
    getDBCollection().updateById(pAction.getId(), pAction);
  }

  @Override
  public Action getAction(String pId) {
    return getDBCollection().findOneById(pId);
  }

  @Override
  public List<Action> getActions() {
    return getDBCollection().find().toArray();
  }

  @Override
  public List<Action> findActionsByCategories(List<String> pCategory)
      throws DatabaseException {
    List<Action> actions = new ArrayList<Action>();
    for (String id : pCategory) {
      actions.addAll(getDBCollection().find().is("category", id).toArray());
    }
    return actions;

  }
}
